#include "Bullet.h"
#include "ConfigManager.h"

void Bullet::SetVelocity(const Vector2 &velocity)
{
    this->velocity = velocity;
    const double pi = 3.14159265;
    if (canRotated) {
        double randian = std::atan2(velocity.y, velocity.x); // 弧度
        angleAnimRotated = randian * 180 / pi;
    }
}

void Bullet::SetPosition(const Vector2 &val)
{
    this->position = val;
}

void Bullet::SetDamage(double val)
{
    this->damage = val;
}

const Vector2 &Bullet::GetSize() const
{
    return size;
}

const Vector2 &Bullet::GetPosition() const
{
    return position;
}

double Bullet::GetDamage() const
{
    return damage;
}

double Bullet::GetDamageRange() const
{
    return damageRange;
}

void Bullet::DisableCollide()
{
    isCollisional = false;
}

bool Bullet::CanCollide() const
{
    return isCollisional;
}

void Bullet::MakeInvalid()
{
    isValid = false;
    isCollisional = false;
}

bool Bullet::CanRemove() const
{
    return !isValid;
}

void Bullet::OnUpdate(double delta)
{
    animation.OnUpdate(delta);
    position += velocity * delta;

    static const SDL_Rect &rect_map
            = ConfigManager::Instance().rectTileMap;

    if (position.x - size.x / 2 <= rect_map.x
        || position.x + size.x / 2 >= rect_map.x + rect_map.w
        || position.y - size.y / 2 <= rect_map.y
        || position.y + size.y / 2 >= rect_map.y + rect_map.h) {
        isValid = false;
    }
}

void Bullet::OnRender(SDL_Renderer *renderer)
{
    static SDL_Point point;

    point.x = (int) (position.x - size.x / 2);
    point.y = (int) (position.y - size.y / 2);

    animation.OnRender(renderer, point, angleAnimRotated);
}

void Bullet::OnCollide(Enemy *enemy)
{
    isValid = false;
    isCollisional = false;
}